/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
#include "system.h"

TaskHandle_t task_dis_handler;	//显示任务
TaskHandle_t task_net_handler;	//4G通讯任务
TaskHandle_t task_485_handler;	//485通讯任务
TaskHandle_t task_t_h_handler;	//温湿度任务
TaskHandle_t task_tmr_handler;	//自检+风速+pm2.5

xQueueHandle remote_con_queue;	//远程控制
xQueueHandle net_rd_queue;			//net数据接收
xQueueHandle air_quality_queue;	//pm2.5和voc
xQueueHandle t_h_queue;					//温度和湿度
xQueueHandle rs485_queue;		//RS485接收

void system_init(void)
{
	//bootload偏移量后移
	//nvic_vector_table_set(NVIC_VECTTAB_FLASH, APP_START_ADDR&0xFFFF);
	//系统时钟配置
	system_clock_config();
	//设置NVIC优先级分组
  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	//外设初始化
	
	
	fldrv_board_init();					//外设IO口初始化
	fldrv_uart_init();					//串口*4初始化
	fldrv_i2c_init();						//模拟IIC初始化
	fldrv_adc_init();						//自检功能初始化
	fldrv_pwm_init();						//风扇控制和风速获取
	flapp_t_h_init();						//温湿度传感器初始化
	flapp_display_init();				//显示初始化
	flapp_save_init();					//falsh初始化
	flapp_485_init();						//判断主机还是从机
}



/* gui task function */
void create_dis_task_function(void *pvParameters)
{
  while(1)
  {
		flapp_dis_run();
  }
}

void create_net_task_function()
{
	while(1)
  {
		flapp_net_run();
		vTaskDelay(TASK_NET_DLY);
	}
}

void create_rs485_task_function()
{
	while(1)
  {
		flapp_rs485_run();
		vTaskDelay(TASK_RS485_DLY);
	}
}

void create_t_h_task_function()
{
	while(1)
  {
		flapp_t_h_run();
		vTaskDelay(TASK_T_H_DLY);
	}
}

void create_tmr_task_function()
{
	while(1)
  {
		flapp_tmr_run();
		vTaskDelay(TASK_TIMER_DLY);
	}
}
/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
	system_init();
  /* enter critical */
	
  taskENTER_CRITICAL();
	//创建消息队列
	rs485_queue 		= xQueueCreate(1,RS485_MOSI_ENUM_END);
	remote_con_queue 	= xQueueCreate(1,REMOTE_NUM);
	net_rd_queue 			= xQueueCreate(1,UART1_RX_BUF);
	air_quality_queue = xQueueCreate(1,sizeof(uint32_t));
	t_h_queue 				= xQueueCreate(1,sizeof(uint32_t));
	
  /*显示任务创建*/
  xTaskCreate((TaskFunction_t )create_dis_task_function,		//函数
								 (const char*    )"display_task",		//任务名
                 (uint16_t       )128,							//堆栈大小
                 (void*          )NULL,							//参数
                 (UBaseType_t    )0,								//优先级
                 (TaskHandle_t*  )&task_dis_handler);		//任务句柄
	/*网络任务创建*/
  xTaskCreate((TaskFunction_t )create_net_task_function,		//函数
								 (const char*    )"net_task",		//任务名
                 (uint16_t       )512,							//堆栈大小
                 (void*          )NULL,							//参数
                 (UBaseType_t    )1,								//优先级
                 (TaskHandle_t*  )&task_net_handler);		//任务句柄
	/*485任务创建*/
  xTaskCreate((TaskFunction_t )create_rs485_task_function,	//函数
								 (const char*    )"rs485_task",		//任务名
                 (uint16_t       )128,							//堆栈大小
                 (void*          )NULL,							//参数
                 (UBaseType_t    )2,								//优先级
                 (TaskHandle_t*  )&task_485_handler);		//任务句柄
	/*温湿度任务创建*/
  xTaskCreate((TaskFunction_t )create_t_h_task_function,		//函数
								 (const char*    )"t_h_task",		//任务名
                 (uint16_t       )64,							//堆栈大小
                 (void*          )NULL,							//参数
                 (UBaseType_t    )3,								//优先级
                 (TaskHandle_t*  )&task_t_h_handler);		//任务句柄
	/*其他任务创建*/
  xTaskCreate((TaskFunction_t )create_tmr_task_function,		//函数
								 (const char*    )"tmr_task",		//任务名
                 (uint16_t       )512,							//堆栈大小
                 (void*          )NULL,							//参数
                 (UBaseType_t    )4,								//优先级
                 (TaskHandle_t*  )&task_tmr_handler);		//任务句柄

  /* exit critical */
  taskEXIT_CRITICAL();

  /* start scheduler */
  vTaskStartScheduler();
}
